#include <QDebug>

#include <databasemanager.h>

#include "videopageparser.h"
#include "constants.h"

namespace Jcr
{
class VideoPageParserPrivate
{
public:
    QString videoLink;
};

VideoPageParser::VideoPageParser(QObject* parent) : ModelPageParser(parent)
{
    setUrl(QUrl(Jcr::TESTVIDEOURL));
    d_ptr = new VideoPageParserPrivate;
}

VideoPageParser::~VideoPageParser()
{
    delete d_ptr;
}

void VideoPageParser::processNode(GumboNode* node)
{
    if (node->v.element.tag == GUMBO_TAG_EMBED) {
        GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "src");
        d_ptr->videoLink = QString(attr->value);
        qDebug() << "video link: " << d_ptr->videoLink;
    }
}

void VideoPageParser::finalize()
{
    if (!d_ptr->videoLink.isEmpty() && !modelName().isEmpty())
        Jcr::DatabaseManager::instance()->addModelVideoLink(this->modelName(), this->d_ptr->videoLink);
}

} // namespace Jcr
